package com.dongdongshop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TemplateService;
import com.dongdongshop.vo.SpecJsonVo;
import com.dongdongshop.vo.SpecVo;
import com.dongdongshop.vo.TypeTemplateVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsEditController")
public class GoodsEditController {

    @DubboReference
    private ItemService itemService;
    @DubboReference
    private TemplateService templateService;
    @DubboReference
    private GoodsService goodsService;
    @DubboReference
    private SpecificationService specificationService;

    //获取分类
    @RequestMapping("getOne")
    @ResponseBody
    public AjaxResult getOne(Long id) {
        if(id == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            List<TbItemCat> one = itemService.getOne(id);
            return AjaxResult.OK().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取模板信息
    @RequestMapping("getTemplate")
    @ResponseBody
    public AjaxResult getBrand(Long id){
        if (id == null && id == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbTypeTemplate template = templateService.getTypeTemplateById(id);

            TypeTemplateVo templateVo = new TypeTemplateVo();
            //拷贝属性
            //BeanUtils.copyProperties(template,templateVo);
            templateVo.setId(template.getId());
            templateVo.setName(template.getName());
            templateVo.setBrandIds(template.getBrandIds());
            templateVo.setCustomAttributeItems(template.getCustomAttributeItems());

            String specIds = template.getSpecIds();
            List<SpecJsonVo> specJsonVos = JSONObject.parseArray(specIds, SpecJsonVo.class);

            for (SpecJsonVo specJsonVo : specJsonVos) {
                SpecVo specVo = specificationService.specificationService(specJsonVo.getId());
                specJsonVo.setOptionList(specVo.getOptionList());
            }
            templateVo.setSpecVoList(specJsonVos);
            return AjaxResult.OK().setData(templateVo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //添加商品
    @RequestMapping("insertGoods")
    @ResponseBody
    public AjaxResult insertGoods(TbGoods goods, TbGoodsDesc goodsDesc,String tbItem) {
        if (goods == null && goodsDesc == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {

            List<TbItem> items = JSONArray.parseArray(tbItem, TbItem.class);

            goodsService.insertGoods(goods, goodsDesc,items);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
